package com.medical.goods.listen;

import com.medical.common.to.OrderTo;
import com.medical.goods.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
@Component
@RabbitListener(queues = {"seckill.order.queue"})
public class SeckillGoodsListen {
    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void listen(OrderTo orderTo, Channel channel, Message message) throws IOException {
        System.out.println("收到生成订单请求，正在生成订单");

        orderService.saveSeckillOrder(orderTo);
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            //确认机制失败，开始重新监听
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }
}
